﻿Imports Microsoft.VisualBasic

Public Class BaseUserControl
    Inherits System.Web.UI.UserControl
    Public Sub objSetFocus(ByVal obj As System.Web.UI.WebControls.WebControl)
        RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & obj.ClientID & "').focus(); "", 10);")

    End Sub
    Protected Property dbName() As String
        Get
            If Session("dbName") = Nothing Then
                Return ""
            End If
            Return CType(Session("dbName"), String)
            'Return "AA"
        End Get
        Set(ByVal value As String)
            Session("dbName") = value
        End Set
    End Property

    ''' <summary>
    ''' 取得登入者姓名
    ''' </summary>
    ''' <value></value>
    ''' <returns>取得登入者姓名</returns>
    ''' <remarks>取得登入者姓名</remarks>
    Protected ReadOnly Property ActualUserName() As String
        Get
            If Session("ActualUserName") = Nothing Then
                Return ""
            End If

            Return CType(Session("ActualUserName"), String)
        End Get

    End Property

    Protected ReadOnly Property ActualUID() As String
        Get
            If Session("ActualUID") = Nothing Then
                Return ""
            End If
            Return CType(Session("ActualUID"), String)
        End Get
    End Property
#Region "runClientScript:從Server端寫出Client端執行的程式碼"
    Public Shared Sub RunClientScript(ByVal obj As Object, ByVal Script As String)
        Dim cMsgr As ClientScriptManager = CType(obj, Page).ClientScript
        Dim strKey As String = GetNewKey("RCS")
        cMsgr.RegisterStartupScript(obj.GetType, strKey, Script, True)
    End Sub

    Public Shared Sub RunClientScriptForAjax(ByVal obj As Page, ByVal Script As String)
        Dim strKey As String = GetNewKey("RSCA")
        ScriptManager.RegisterStartupScript(obj, obj.GetType(), strKey, Script, True)
    End Sub
#End Region

    ''' <summary>
    ''' 秀出訊息
    ''' </summary>
    ''' <param name="msg"></param>
    ''' <remarks>秀出訊息</remarks>
    Public Sub ShowMessage(ByVal msg As String)
        Dim tmp As String = msg.Replace("'", "")
        tmp = tmp.Replace(Chr(13), " "c)
        tmp = tmp.Replace(Chr(10), " "c)
        tmp = tmp.Replace(Chr(9), " "c)
        'tmp = HttpUtility.HtmlEncode(tmp)
        Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "ShowMessage", "<script language=JavaScript> alert( '" + tmp.Trim() + "');</script>")

    End Sub

    ''' <summary>
    ''' 有UpdatePanel時秀出訊息
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <param name="Msg"></param>
    ''' <remarks>有UpdatePanel時秀出訊息</remarks>
    Public Shared Sub ShowMessageForAjax(ByVal obj As Object, ByVal Msg As String)


        ScriptManager.RegisterStartupScript(obj, GetType(BasePage), "ShowMessage", "alert(""" & Msg.Replace("\", "\\").Replace("""", "\""").Replace(vbCrLf, "\n\r") & """);", True)


    End Sub

#Region "GetNewKey : 取的唯一的名稱"
    ''' <summary>
    ''' 取的唯一的名稱
    ''' </summary>
    ''' <param name="Kind">Prefix name</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetNewKey(ByVal Kind As String) As String
        Randomize()
        Dim intRandomNumber As Integer = Rnd() * 100000
        Return Kind & Now.ToString("yyyyMMddHHmmss") & Now.Millisecond.ToString("000") & intRandomNumber.ToString()
    End Function


#End Region
End Class
